package games.gong.durid.mapper;

import games.gong.durid.entity.UserRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserRoleMapper {
    
    @Insert("INSERT INTO user_role(user_id, role_id, create_time) " +
            "VALUES(#{userId}, #{roleId}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserRole userRole);

    @Delete("DELETE FROM user_role WHERE id = #{id}")
    int deleteById(Long id);
    
    @Delete("DELETE FROM user_role WHERE user_id = #{userId} AND role_id = #{roleId}")
    int deleteByUserIdAndRoleId(@Param("userId") Long userId, @Param("roleId") Long roleId);

    @Select("SELECT * FROM user_role WHERE id = #{id}")
    @Results({
        @Result(property = "createTime", column = "create_time")
    })
    UserRole selectById(Long id);
    
    @Select("SELECT * FROM user_role WHERE user_id = #{userId}")
    @Results({
        @Result(property = "createTime", column = "create_time")
    })
    List<UserRole> selectByUserId(Long userId);
    
    @Select("SELECT * FROM user_role WHERE role_id = #{roleId}")
    @Results({
        @Result(property = "createTime", column = "create_time")
    })
    List<UserRole> selectByRoleId(Long roleId);
}